package com.heisenhub.file.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heisenhub.common.db.service.impl.SuperServiceImpl;
import com.heisenhub.file.domin.FileStorageManager;
import com.heisenhub.file.entity.OpFile;
import com.heisenhub.file.mapper.OpFileMapper;
import com.heisenhub.file.query.OpFileQuery;
import com.heisenhub.file.service.IOpFileService;
import com.heisnenhub.common.module.exception.RRException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sen
 * @since 2024-03-06
 */
@Service
public class OpFileServiceImpl extends SuperServiceImpl<OpFileMapper,  OpFile, OpFileQuery> implements IOpFileService {

    @Lazy
    @Autowired
    FileStorageManager fileStorageManager;
    public IPage<OpFile> doPage(OpFileQuery pageQuery) {
        Page page = new Page(pageQuery.getPageNo(), pageQuery.getPageSize());
        IPage<OpFile> opFileIPage = baseMapper.doPage(page, pageQuery);
        List<OpFile> records = opFileIPage.getRecords();
        records.forEach( file ->  {
            String url = fileStorageManager.fileUrl(file.getStoreService(),file.getStorePath());
            file.setFileUrl(url);
        });
        return page;
    }
    @Override
    public OpFile getFileByFileId(String fileId) {
        if(StringUtils.isEmpty(fileId)) throw new RRException("fileId  不能为空");
        OpFile byId = this.getById(fileId);
        if(byId == null) throw new RRException("文件对象未找到");
        return byId;
    }
}
